package com.carbon.controller;


import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.common.core.domain.AjaxResult;
import com.carbon.domain.Carrier;
import com.carbon.domain.Delivery;
import com.carbon.vo.SearchParams;
import com.carbon.service.DeliveryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 配送管理表(Delivery)表控制层
 *
 * @author makejava
 * @since 2023-09-05 16:32:42
 */
@RestController
@Slf4j
@RequestMapping("/wms/delivery")

public class DeliveryController {
    /**
     * 服务对象
     */
    @Resource
    private DeliveryService deliveryService;


    @GetMapping("/list")
    AjaxResult list(SearchParams params){

        Page<Delivery> page = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaQueryWrapper<Delivery> lqw=new LambdaQueryWrapper<Delivery>()
                .eq(ObjectUtil.isNotEmpty(params.getDistributionStatus()), Delivery::getDistributionStatus, params.getDistributionStatus());
        String search = params.getSearch();
        if (ObjectUtil.isNotEmpty(search)) {
            lqw.like(Delivery::getOrderId, search).or()
                    .like(Delivery::getDispatchId, search).or()
                    .like(Delivery::getCarNumber, search).or()
                    .like(Delivery::getCustomerName, search).or()
                    .like(Delivery::getDeliveryAddr, search);
        }
        String dateTime = params.getDateTime();
        if (ObjectUtil.isNotEmpty(dateTime)) {
            lqw.like(Delivery::getDistributionTime, dateTime).or()
                    .like(Delivery::getArriveTime, dateTime);
        }
        page = deliveryService.page(page, lqw);
        List<Delivery> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return AjaxResult.success();
        }
        return AjaxResult.success()
                .put("rows", records)
                .put("total", page.getTotal());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        Delivery delivery = deliveryService.getById(id);
        return AjaxResult.success(delivery);
    }

    /**
     * 新增数据
     *
     * @param delivery 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody Delivery delivery) {
        AjaxResult result = IsLegitiMate(delivery);
        if (result != null) {
            return result;
        }
        deliveryService.save(delivery);
        return AjaxResult.success();
    }
    /**
     * 参数校验
     *
     * @param delivery
     * @return
     */
    private static AjaxResult IsLegitiMate(Delivery delivery) {
        if (ObjectUtil.isEmpty(delivery.getCarNumber())) {
            return AjaxResult.error("车牌号为空");
        }
        if (ObjectUtil.isEmpty(delivery.getDistributionTime())) {
            return AjaxResult.error("配送时间为空");
        }
        if (ObjectUtil.isEmpty(delivery.getDeliveryAddr())) {
            return AjaxResult.error("配送地址为空");
        }

        return null;
    }
    /**
     * 修改数据
     *
     * @param delivery 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody Delivery delivery) {
        deliveryService.updateById(delivery);
        return AjaxResult.success();
    }

    /**
     * 删除数据
     *
     * @param id
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        deliveryService.removeById(id);
        return AjaxResult.success();
    }
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(int id) {
        Delivery delivery=new Delivery();
        delivery.setCheckId(id);
        delivery.setDistributionStatus("0");
        return deliveryService.updateById(delivery) ? AjaxResult.success() : AjaxResult.error();
    }
    @PutMapping("/updateStatus2")
    public AjaxResult updateStatus2(int id) {
        Delivery delivery=new Delivery();
        delivery.setCheckId(id);
        delivery.setDistributionStatus("1");
        return deliveryService.updateById(delivery) ? AjaxResult.success() : AjaxResult.error();
    }
}


